package com.javalab.recorder.state;

import com.javalab.recorder.audio.AudioCache;
import com.javalab.recorder.audio.AudioCaptureThread;
import com.javalab.recorder.audio.AudioPlaybackThread;

public class StoppedState extends AbstractRecorderState {

	private static StoppedState state;

	private AudioCaptureThread audioCaptureThread;

	private AudioPlaybackThread audioPlaybackThread;

	private StoppedState() {

	}

	public static StoppedState getInstance() {
		synchronized (StoppedState.class) {
			if (state == null) {
				state = new StoppedState();
			}
			return state;
		}
	}

	@Override
	public boolean isRecordButtonEnabled() {
		return true;
	}

	@Override
	public boolean isPlaybackButtonEnabled() {
		if (AudioCache.getInstance().get("CapturedAudioData") == null) {
			return false;
		}
		return true;
	}

	@Override
	public boolean isStopButtonEnabled() {
		return false;
	}

	@Override
	public void doRecording() {
		audioCaptureThread.startCapturing();
		if (mainFrame != null) {
			mainFrame.setRecorderState(RecordingState.getInstance());
		}
	}

	@Override
	public void doPlaying() {
		if (mainFrame != null) {
			mainFrame.setRecorderState(PlayingState.getInstance());
		}
		byte[] data = (byte[]) AudioCache.getInstance().get("CapturedAudioData");
		audioPlaybackThread.setAudioData(data);
		audioPlaybackThread.startPlaying();
	}

	@Override
	public void doStopping() {

	}

	public AudioCaptureThread getAudioCaptureThread() {
		return audioCaptureThread;
	}

	public void setAudioCaptureThread(AudioCaptureThread audioCaptureThread) {
		this.audioCaptureThread = audioCaptureThread;
	}

	public AudioPlaybackThread getAudioPlaybackThread() {
		return audioPlaybackThread;
	}

	public void setAudioPlaybackThread(AudioPlaybackThread audioPlaybackThread) {
		this.audioPlaybackThread = audioPlaybackThread;
	}

}
